package org.brisling.supply.queryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.DateUtil;
import org.brisling.supply.domain.BaseSupplyInfo;
import org.brisling.supply.service.BaseSupplyInfoService;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <p>Title: QueryBasePatientInfoService</p>
 * <p>Description: 供应商诚信度质询统计查询service</p>
 * <p>Company: tongking</p>
 * @author trastor
 * @since 1.0
 * @version 1.0
 * @date 2016年7月20日 下午10:02:55
 */
@RestController
@RequestMapping("/baseSupplyInfo/query")
public class QueryBaseSupplyInfoService extends AbstractBaseQueryService<BaseSupplyInfo>{

	@Autowired
	
	private BaseSupplyInfoService baseSupplyInfoService;
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	
	@Autowired
	private QueryDictionaryService qryDictionaryService;
	
	
	public QueryBaseSupplyInfoService(){
		this.moduleName = ConstSysParam.MODULE_SUPPLY_COUNTINFO;
		this.subSystem = ConstSysParam.SUBSYSTEM_SUPPLY;
		this.tableName = this.moduleName;
		this.clazz = BaseSupplyInfo.class;
		typeMap = this.getTypeMap(clazz);
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	/**
	 * 供应商基本信息、申请审核、已经审核供应商查询弹窗
	 * 
	 */
	
	@RequestMapping("/popPage.action")
	public ModelAndView getPopPage(){
		String userid = shiroSecurityService.getCurrentUserIdentify();
		String _page = "popMain";
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_medical_all")){
			if(!isManageJob(userid)){				
				_page="popDetail";
			}					
		}
		return new ModelAndView("/supply/supply_baseinfo_pop_info/"+_page);
	}
	
	
	/**
	 * 供应商基本信息不全查询弹窗
	 * 
	 */
	
	@RequestMapping("/lackInfoPopPage.action")
	public ModelAndView getLackInfoPopPage(){
		String userid = shiroSecurityService.getCurrentUserIdentify();
		String _page = "popLackInfoMain";
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_medical_all")){
			if(!isManageJob(userid)){				
				_page="popDetail";
			}					
		}
		return new ModelAndView("/supply/supply_baseinfo_pop_info/"+_page);
	}
	
	
	/**
	 * 供应商档案不齐全查询弹窗
	 * 
	 */
	
	@RequestMapping("/lackArchivePopPage.action")
	public ModelAndView getLackArchivePopPage(){
		String userid = shiroSecurityService.getCurrentUserIdentify();
		String _page = "popLackArchiveMain";
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_medical_all")){
			if(!isManageJob(userid)){				
				_page="popDetail";
			}					
		}
		return new ModelAndView("/supply/supply_baseinfo_pop_info/"+_page);
	}
	

	/**
	 * 供应商档案不齐全查询弹窗
	 * 
	 */
	
	@RequestMapping("/badInfoPopPage.action")
	public ModelAndView getBadInfoPopPage(){
		String userid = shiroSecurityService.getCurrentUserIdentify();
		String _page = "popBadInfoMain";
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_medical_all")){
			if(!isManageJob(userid)){				
				_page="popDetail";
			}					
		}
		return new ModelAndView("/supply/supply_baseinfo_pop_info/"+_page);
	}
	
	
	

	/**
	 * 统计供应商监控汇总数据
	 * @param body
	 * @return
	 */
	@RequestMapping("/getListSupplyInfo.action")
      public ListDomain<BaseSupplyInfo> getAllListDomain(@RequestBody(required=false) String body){		
		
		Map<String,Object> map = this.getBodyParam(body);
		
//		String userid = shiroSecurityService.getCurrentUserIdentify();
//		Map<String,String> paramMap = (Map<String, String>) map.get(ConstSysParam.CONDITION_MAP_KEY);
		//查询表 
		return super.getListDomainByConditionAndOrder(null, map, typeMap, orderMap, true);
		
	}
	
	
	
	

	
	public void autoTask(){
		Integer year=null;
		Integer month=null;
		this.autoMonitor(year, month);
	}
	
	
	/**
	 * 执行自动统计
	 */
	public void autoMonitor(Integer year,Integer month){
		String[] yestodayMonth=null ;
		
		if(year ==null || month ==null)
			yestodayMonth = DateUtil.getYestodayMonthDay();
		else{
			yestodayMonth = DateUtil.getMonthDay(year, month);
		}
		//获得当前供应商质询统计数据
		List<BaseSupplyInfo> inlists = countSupplyBaseInfo(yestodayMonth[2],yestodayMonth[0],yestodayMonth[1]);
		//查询已有数据
		BaseSupplyInfo oldBaseSupplyInfo = super.getOneByIdentify("monitor_year_month", yestodayMonth[2]);
		
		if(inlists!=null){
			BaseSupplyInfo baseSupplyInfo =new BaseSupplyInfo();
				for(int i=0;i<inlists.size();i++){
					BaseSupplyInfo b = inlists.get(i);
					if(b.getSupply_num()!=null)
						baseSupplyInfo.setSupply_num(b.getSupply_num());
					else if(b.getSupply_badinfo_num()!=null){
						baseSupplyInfo.setSupply_badinfo_num(b.getSupply_badinfo_num());
					}
					else if(b.getApply_supply_audit_num()!=null){
						baseSupplyInfo.setApply_supply_audit_num(b.getApply_supply_audit_num());
					}
					else if(b.getPass_supply_audit_num()!=null){
						baseSupplyInfo.setPass_supply_audit_num(b.getPass_supply_audit_num());
					}
					else if(b.getLack_supply_archive_num()!=null){
						baseSupplyInfo.setLack_supply_archive_num(b.getLack_supply_archive_num());
					}
					else if(b.getLack_supply_baseinfo_num()!=null){
						baseSupplyInfo.setLack_supply_baseinfo_num(b.getLack_supply_baseinfo_num());
					}
					
				}
				//如果当月数据已经存在，则获取根据id值对当月的数据进行更新，如果没有则插入一条新数据(按月进行更新)
				if(oldBaseSupplyInfo.getId()!=null){
					baseSupplyInfo.setId(oldBaseSupplyInfo.getId());
				}
				baseSupplyInfo.setMonitor_year_month(yestodayMonth[2]);
				baseSupplyInfoService.save(baseSupplyInfo);
			}
			
	}
	
	
	public void autoUpdateStandard(Integer year,Integer month){
		String[] yestodayMonth = null;
		if(year ==null || month ==null)
			yestodayMonth = DateUtil.getYestodayMonthDay();
		else{
			yestodayMonth = DateUtil.getMonthDay(year, month);
		}
		baseSupplyInfoService.updateStandard(yestodayMonth[2]);
	}
	
	

	/**
	 * 统计基本药物住院缓存信息
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public List<BaseSupplyInfo> countSupplyBaseInfo(String year_month,String startDate ,String endDate){
		
			
		
		String[] sqlids = {"supply_base_info_all","apply_supply_audit_num",
				"pass_supply_audit_num","lack_supply_baseinfo_num",
				"lack_supply_archive_num","supply_badinfo_num"};
		
//		String[] tempString={"select count(*) as supply_num from supply_baseinfo",
//				"select count(*) as supply_badinfo_num  from (select base.ID,BASE.SUP_NAME  from   SUPPLY_BADINFO bad left join SUPPLY_BASEINFO base on BASE.ID = BAD.SUPPLY_ID)",
//				""};
		
		//统计供应商监控模块全部数据
		
		List<String> sqls = new ArrayList<String>();
		for(int i = 0;i<sqlids.length;i++){
			String sql = qryDictionaryService.getSqlByBizcode(sqlids[i]);
			if(sql!=null){
				sqls.add(sql);
			}
		}
		//所有数据
		List<BaseSupplyInfo> lists = super.monitorCountBase(year_month,startDate,endDate,sqls);		
		return lists;
		
	}	
	
	
	/**
	 * 获取供应商质询统计指标
	 * @param body
	 * @return
	 */
	
		
	
}
